package com.sport.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StringListDeserializer extends JsonDeserializer<List<String>> {

    @Override
    public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        ObjectMapper mapper = (ObjectMapper) p.getCodec();
        JsonNode node = mapper.readTree(p);
        List<String> result = new ArrayList<>();

        // 添加日志信息
        System.out.println("Node: " + node);
        System.out.println("Node type: " + node.getNodeType());

        if (node.isArray()) {
            for (JsonNode item : node) {
                if (item.isTextual()) {
                    result.add(item.textValue());
                } else if (item.isNumber()) {
                    result.add(item.numberValue().toString());
                } else if (item.isNull()) {
                    result.add(null);
                } else {
                    result.add(item.asText());
                }
            }
        } else if (node.isTextual()) {
            result.add(node.textValue());
        } else if (node.isNumber()) {
            result.add(node.numberValue().toString());
        } else if (node.isNull()) {
            return result;
        } else {
            result.add(node.asText());
        }

        // 添加日志信息
        System.out.println("Deserialized " + node + " to " + result);

        return result;
    }

}
